<!-- 评论 -->
<template>
  <div id="comment" v-if="!deleteSelf">
    <div class="comment-container">
      <div class="comment-header">
        <img :src="comment.userinfo.userProfile" @click="toUserCenter()" />
        <h4>{{ comment.userinfo.nickname }}</h4>
        <span>{{ comment.commentTime }}</span>
      </div>
      <div class="comment-content" v-html="comment.commentContent"></div>
      <div class="comment-footer">
        <div class="fave-container" @click="handleLike()">
          <favebutton :status="likeStatus"></favebutton>
          <span class="fave-times">{{ likeNumber }}</span>
        </div>
        <div>
          <div class="fave-container" @click="handleCollect()">
            <collectionbutton :status="collectStatus"></collectionbutton>
            <span class="fave-times">{{ collectNumber }}</span>
          </div>
          <div class="fave-container" @click="openComment()">
            <commentbutton :status="false"></commentbutton>
          </div>
          <div class="fave-container" @click="handleReport()">
            <reportbutton :status="false"></reportbutton>
          </div>
        </div>
        <div
          class="delete-button-container"
          v-if="user.userNo == comment.userinfo.userNo"
          @click="deleteComment()"
        >
          <deletebutton :status="false"></deletebutton>
        </div>
      </div>

      <!-- 次级评论区 -->
      <div class="others-comments" v-if="subCommentsVis">
        <div class="comments">
          <subcomment
            v-for="subcomment in subComments"
            :key="subcomment.commentNo"
            :comment="subcomment"
            :commentfooter="true"
            :fatherCommentNo="comment.commentNo"
          ></subcomment>
        </div>
        <div class="comment-input-container">
          <div class="post-comment-input">
            <commentinput
              v-model="subCommentContent"
              :commentContent="subCommentContent"
              v-if="commentInputShow"
            ></commentinput>
          </div>
          <button class="post-comment-button" @click="sendComment()">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-cmfabu"></use>
            </svg>
            发布
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Collectionbutton from "../collectionbutton.vue";
import Favebutton from "../favebutton.vue";
import Deletebutton from "../littlebutton/deletebutton.vue";
import Commentbutton from "../littlebutton/commentbutton";
import Reportbutton from "../littlebutton/reportbutton.vue";
import commentinput from "./commentinput.vue";
import { mapGetters } from "vuex";
import {
  handleLike,
  handleCollect,
} from "../../../requestsetting/uniOperation";
import Subcomment from "./subcomment.vue";
// eslint-disable-next-line no-unused-vars
import { intializeComments, handleComments } from "../../../utils/utils";

export default {
  data() {
    return {
      likeNumber: 0,
      collectNumber: 0,
      likeStatus: false,
      collectStatus: false,
      deleteSelf: false,
      subCommentsVis: false,
      subCommentContent: "",
      commentInputShow: true, //使输入框刷新，
      subComments: "",
    };
  },

  props: ["comment"],

  components: {
    Favebutton,
    Collectionbutton,
    Reportbutton,
    Deletebutton,
    Commentbutton,
    commentinput,
    Subcomment,
  },

  computed: {
    ...mapGetters(["user"]),
  },

  methods: {
    deleteComment() {
      //删除评论
      this.$getApi.deleteComment(this.comment.commentNo);
      this.deleteSelf = true;
    },
    toUserCenter() {
      //跳转至指定用户中心
      this.$router.push({
        path: "/usercenter",
        query: { userNo: this.comment.userNo },
      });
    },
    handleLike() {
      //处理点赞操作
      handleLike(this.user.userNo, this.comment.commentNo, 2);
      if (
        this.likeStatus == false ||
        this.likeNumber == 0 ||
        this.likeStatus == undefined
      )
        this.likeNumber = this.likeNumber + 1;
      else this.likeNumber = this.likeNumber - 1;
      this.likeStatus = !this.likeStatus;
    },
    handleCollect() {
      //处理收藏操作
      handleCollect(this.user.userNo, this.comment.commentNo, 2);
      if (
        this.collectStatus == false ||
        this.collectStatus == 0 ||
        this.collectStatus == undefined
      )
        this.collectNumber = this.collectNumber + 1;
      else this.collectNumber = this.collectNumber - 1;
      this.collectStatus = !this.collectStatus;
    },
    handleReport() {},

    openComment() {
      //打开次级评论区
      this.getSubComments();
      this.subCommentsVis = !this.subCommentsVis;
    },
    getSubComments() {
      const that = this;
      this.$getApi
        .getAllSubComments(this.comment.commentNo, this.user.userNo)
        .then((res) => {
          let commentstemp = intializeComments(
            //为评论添加发送者信息
            res.data.comments,
            res.data.user2,
            res.data.user1
          );
          that.subComments = handleComments(
            //为评论添加点赞收藏信息
            commentstemp,
            res.data.like,
            res.data.collection
          );
        });
    },
    sendComment() {
      let newdate = new Date();
      let commentTime = newdate.toLocaleString("chinese", { hour12: false });
      commentTime = commentTime.replace(/\//g, "-");
      let commentContentTemp = this.subCommentContent.replace(
        /[\n\r]/g,
        "<br>"
      );
      console.log(commentContentTemp);

      let config = {
        baseURL: "http://192.168.0.136:8080/",
      };
      const that = this;
      this.$getApi.commentCheck(commentContentTemp, config).then((res) => {
        console.log("审核评论", res);
        if (res != 0) {
          that.$toast("请注意友善发言");
        }
      });

      // let that = this;
      this.subCommentContent = "";
      this.commentInputShow = false;
      this.$nextTick(() => {
        // dom元素更新后执行，因此这里能正确打印更改之后的值
        this.commentInputShow = true;
      });
      // eslint-disable-next-line no-unused-vars
      this.$getApi
        .addComment(
          this.$route.query.postNo,
          this.user.userNo,
          commentTime,
          commentContentTemp,
          this.comment.commentNo, //被回复者编号
          this.comment.commentNo
        )
        .then(() => {
          that.getSubComments();
        });
    },
  },
  created() {
    console.log(123, this.comment);
    this.likeStatus = this.comment.likeStatus;
    this.collectStatus = this.comment.collectStatus;
    this.likeNumber = this.comment.commentLikeNumber;
    this.collectNumber = this.comment.commentCollectionNumber;
  },
};
</script>
<style>
#comment {
  position: relative;
  width: 100%;
  background-color: white;
  border-bottom: 1px rgb(241, 247, 255) solid;
  transition: all 0.4s ease;
  animation: show 0.4s ease;
}
.comment-container {
  position: relative;
  min-height: 30px;
}
.comment-header {
  position: relative;
  width: calc(100% - 20px);
  margin: 0 auto;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
.comment-header > img {
  width: 30px;
  height: 30px;
  border-radius: 15px;
  background-position: center;
  cursor: pointer;
}
.comment-header > h4 {
  margin-left: 8px;
  font-weight: 500;
}
.comment-header > span {
  position: absolute;
  right: 0;
  color: rgb(152, 152, 152);
  font-size: 14px;
}
.comment-content {
  width: calc(100% - 60px);
  word-wrap: break-word;
  margin: 10px auto;
  line-height: 1.67;
  font-size: 15px;
  color: rgb(49, 49, 49);
}
.comment-footer {
  position: relative;
  width: calc(100% - 60px);
  margin: 0 auto 10px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  box-sizing: border-box;
}
.comment-footer > div:nth-child(2) {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  visibility: hidden;
  opacity: 0;
  transition: all 0.3s;
}
.comment-footer:hover div:nth-child(2) {
  transition: all 0.3s;
  visibility: visible;
  opacity: 1;
}
.comment-footer > .delete-button-container {
  position: absolute;
  right: 0;
}

/* 次级评论容器 */
.others-comments {
  width: calc(100% - 60px);
  margin: 10px auto;
}
.others-comments > .comments {
}
/* 次级评论容器 */

.comment-input-container {
  position: relative;
  width: 100%;
}
.post-comment-input {
  position: relative;
  width: 90%;
}
@keyframes show {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
</style>
